package org.apache.solr.ocean.replication;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.solr.ocean.http.HttpParameters;
import org.apache.solr.ocean.util.CElement;
import org.apache.solr.ocean.util.XMLUtil;
import org.jdom.Element;

public class NodeInfo implements CElement {
  private String id;
  private URL url;
  private String chosenMasterId;
  private String operatingRole;
  private Long highestLogId;

  public NodeInfo(Element element) throws Exception {
    
  }

  public void transferTo(HttpParameters parameters) {
    parameters.add("id", id);
    parameters.add("url", url);
    parameters.add("chosenMasterId", chosenMasterId);
    parameters.add("operatingRole", operatingRole);
    parameters.add("highestLogID", highestLogId);
  }

  public NodeInfo(HttpParameters parameters) throws MalformedURLException {
    id = parameters.get("id");
    url = parameters.getUrl("url");
    chosenMasterId = parameters.get("chosenMasterId");
    operatingRole = parameters.get("operatingRole");
    highestLogId = parameters.getLong("highestLogID");
  }

  public NodeInfo(String id, URL url, String chosenMasterId, String operatingRole, Long highestLogID) {
    this.id = id;
    this.url = url;
    this.chosenMasterId = chosenMasterId;
    this.operatingRole = operatingRole;
    this.highestLogId = highestLogID;
  }

  public Long getHighestLogId() {
    return highestLogId;
  }

  public Element toElement() throws Exception {
    Element element = new Element("nodeinfo");
    XMLUtil.createTextElement("id", id, element);
    XMLUtil.createTextElement("url", url, element);
    XMLUtil.createTextElement("chosenMasterId", chosenMasterId, element);
    XMLUtil.createTextElement("operatingRole", operatingRole, element);
    XMLUtil.createTextElement("highestLogId", highestLogId, element);
    return element;
  }

  public String getId() {
    return id;
  }

  public URL getUrl() {
    return url;
  }

  public String getChosenMasterId() {
    return chosenMasterId;
  }

  public String getOperatingRole() {
    return operatingRole;
  }

  public boolean equals(Object object) {
    if (!(object instanceof NodeInfo))
      return false;
    NodeInfo other = (NodeInfo) object;
    EqualsBuilder equalsBuilder = new EqualsBuilder();
    equalsBuilder.append(id, other.id);
    equalsBuilder.append(url, other.url);
    equalsBuilder.append(chosenMasterId, other.chosenMasterId);
    // equalsBuilder.append(operatingRole, other.operatingRole);
    return equalsBuilder.isEquals();
  }

  public int hashCode() {
    HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(555, 121);
    hashCodeBuilder.append(id);
    hashCodeBuilder.append(url);
    hashCodeBuilder.append(chosenMasterId);
    // hashCodeBuilder.append(operatingRole);
    return hashCodeBuilder.toHashCode();
  }
}